package com.sx.first.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.sx.first.dao.MaterialDAO;
import com.sx.first.model.Material;

@Repository("materialDAO")
@Scope("prototype")
public class MaterialDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<Material> findAll() {
		@SuppressWarnings("unchecked")
		List<Material> list = sessionFactory.getCurrentSession().createQuery("from Material").list();
		return list;
	}

	public void save(Material mat) {
		sessionFactory.getCurrentSession().save(mat);
	}

	public void delete(Material mat) {
		sessionFactory.getCurrentSession().delete(mat);
	}

	public void update(Material mat) {
		sessionFactory.getCurrentSession().update(mat);
	}

	public Material findById(int matId) {
		return sessionFactory.getCurrentSession().get(Material.class, matId);
	}

}
